package com.roadjava.GDOUGym.service.impl;


import com.roadjava.GDOUGym.bean.dto.EquipmentDTO;
import com.roadjava.GDOUGym.bean.dto.EquipmentRentDTO;
import com.roadjava.GDOUGym.bean.dto.GameDTO;
import com.roadjava.GDOUGym.bean.entity.EquipmentDO;
import com.roadjava.GDOUGym.bean.entity.EquipmentRentDO;
import com.roadjava.GDOUGym.bean.entity.GameDO;
import com.roadjava.GDOUGym.bean.entity.GameEquipmentDO;
import com.roadjava.GDOUGym.bean.res.ResultDTO;
import com.roadjava.GDOUGym.convert.EquipmentConvert;
import com.roadjava.GDOUGym.convert.EquipmentRentConvert;
import com.roadjava.GDOUGym.mapper.EquipmentMapper;
import com.roadjava.GDOUGym.mapper.EquipmentRentMapper;
import com.roadjava.GDOUGym.mapper.GameEquipmentMapper;
import com.roadjava.GDOUGym.mapper.GameMapper;
import com.roadjava.GDOUGym.service.EquipmentRentService;
import com.roadjava.GDOUGym.service.EquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class EquipmentRentServiceImpl implements EquipmentRentService {
    @Resource
    private EquipmentRentMapper equipmentRentMapper;

    @Resource
    private EquipmentMapper equipmentMapper;

    @Resource
    private GameEquipmentMapper gameEquipmentMapper;

    @Resource
    private GameMapper gameMapper;

    @Resource
    private EquipmentRentConvert equipmentRentConvert;

    @Resource
    private EquipmentConvert equipmentConvert;



    @Override
    public ResultDTO<List<EquipmentRentDTO>> find(EquipmentRentDTO dto) {
        List<EquipmentRentDO> dos = equipmentRentMapper.find(dto);
        for(int i= 0 ;i<dos.size();i++){
            EquipmentDTO equipmentDTO = new EquipmentDTO();
            equipmentDTO.setEquipmentID(dos.get(i).getEquipmentID());
            List<EquipmentDO> dtoList = equipmentMapper.find(equipmentDTO);
            dos.get(i).setEquipmentName(dtoList.get(0).getEquipmentName());
        }

        List<EquipmentRentDTO> dtos = equipmentRentConvert.dos2dtos(dos);
        long total = equipmentRentMapper.selectCount(dto);
        return ResultDTO.buildSuccess(dtos,total);
    }

    @Override
    public ResultDTO<List<EquipmentDTO>> findCanRent(EquipmentRentDTO dto) {
        //传入数据验证
        EquipmentRentDO equipmentRentDO = equipmentRentConvert.dto2do(dto);
        System.out.println("equipmentRentDO  = " + equipmentRentDO);

        //订单信息
        List<EquipmentDO> equipmentDOList = equipmentRentMapper.findCanRent(dto);
        System.out.println("equipmentDOList  = " + equipmentDOList);


        //返回订单信息
        List<EquipmentDO> resList = new ArrayList<>();
        long total = equipmentRentMapper.selectCanRentCount(dto);

        //返回信息合成
        for(int i=0;i<equipmentDOList.size();i++){
            EquipmentDTO equipmentRentDTO = new EquipmentDTO();
            equipmentRentDTO.setEquipmentID(equipmentDOList.get(i).getEquipmentID());//器材号
            equipmentRentDTO.setEquipmentName(equipmentDOList.get(i).getEquipmentName());//器材名
            equipmentRentDTO.setRent(equipmentDOList.get(i).getRent());//租金
            equipmentRentDTO.setEquipmenTmanager(equipmentDOList.get(i).getEquipmenTmanager());//负责人
            equipmentRentDTO.setRentData(dto.getRentData());//租用时间段
            equipmentRentDTO.setRentTimeSub(dto.getRentTimeSub());//租用时间段
            equipmentRentDTO.setDescription(equipmentDOList.get(i).getDescription());
            equipmentRentDTO.setEquipmentCanApp(equipmentDOList.get(i).getEquipmentSum()-equipmentDOList.get(i).getSum());//可租用数量
            resList.add(equipmentRentDTO);
        }


        List<EquipmentDTO> dtos = equipmentConvert.dos2dtos(resList);

        return ResultDTO.buildSuccess(dtos,total);
    }

    @Override
    public ResultDTO<String> add(EquipmentRentDTO equipmentDTO) {
        EquipmentRentDTO dto = new EquipmentRentDTO();
        String no;
        String no2;

        List<EquipmentRentDO> equipmentRentDTOList = equipmentRentMapper.find(dto);
        if(equipmentRentDTOList.size() == 0){
            no = "0001";
        }else{
            GameDTO gameDTO = new GameDTO();
            List<GameDO> gameDOList = gameMapper.find(gameDTO);
            String endGameNo = gameDOList.get(gameDOList.size()-1).getContesNo();
            long totalGame = Integer.parseInt(endGameNo)+1;

            no = "";
            if(totalGame>=0 && totalGame<10){
                no = "000"+totalGame;
            }else if(totalGame>=10 && totalGame<100){
                no = "00"+totalGame;
            }else if(totalGame>=100 && totalGame<1000){
                no = "0"+totalGame;
            }else{
                no = totalGame+"";
            }
        }


        if(equipmentRentDTOList.size() == 0 ){
            no2 = "0001";
        }else{
            String endNo = equipmentRentDTOList.get(equipmentRentDTOList.size()-1).getRentNo();
            long total = Integer.parseInt(endNo)+1;
            // 把dto转为do对象
            no2 = "";
            if(total>=0 && total<10){
                no2 = "000"+total;
            }else if(total>=10 && total<100){
                no2 = "00"+total;
            }else if(total>=100 && total<1000){
                no2 = "0"+total;
            }else{
                no2 = total+"";
            }


        }

        EquipmentRentDO equipmentRentDO = equipmentRentConvert.dto2do(equipmentDTO);
        equipmentRentDO.setRentNo(no2);
        int result = equipmentRentMapper.add(equipmentRentDO);
        GameEquipmentDO gameEquipmentDO = new GameEquipmentDO();
        gameEquipmentDO.setGameNO(no);
        gameEquipmentDO.setEquipmentNO(no2);
        int resultGame = gameEquipmentMapper.add(gameEquipmentDO);
        if (result == 1 && resultGame == 1 ) {
            return ResultDTO.buildSuccess("预约成功");
        }else {
            return ResultDTO.buildSuccess("预约成功");
        }
    }

    @Override
    public ResultDTO<String> addEquipment(EquipmentRentDTO equipmentDTO) {
        EquipmentRentDTO dto = new EquipmentRentDTO();
        String no;
        List<EquipmentRentDO> equipmentRentDOList = equipmentRentMapper.find(dto);
        if(equipmentRentDOList.size() == 0){
            no = "0001";
        }else{
            String endGameNo = equipmentRentDOList.get(equipmentRentDOList.size()-1).getRentNo();
            long total = Integer.parseInt(endGameNo)+1;
            // 把dto转为do对象

            no = "";
            if(total>=0 && total<10){
                no = "000"+total;
            }else if(total>=10 && total<100){
                no = "00"+total;
            }else if(total>=100 && total<1000){
                no = "0"+total;
            }else{
                no = total+"";
            }
        }

        EquipmentRentDO equipmentRentDO = equipmentRentConvert.dto2do(equipmentDTO);
        equipmentRentDO.setRentNo(no);
        int result = equipmentRentMapper.addEquipment(equipmentRentDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("预约成功");
        }else {
            return ResultDTO.buildSuccess("预约成功");
        }
    }

    @Override
    public ResultDTO<String> change(EquipmentRentDTO equipmentDTO) {
        // 把dto转为do对象
        EquipmentRentDO equipmentRentDO = equipmentRentConvert.dto2do(equipmentDTO);
        int result = equipmentRentMapper.change(equipmentRentDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("修改成功");
        }else {
            return ResultDTO.buildSuccess("修改失败");
        }
    }

    @Override
    public ResultDTO<String> delete(EquipmentRentDTO equipmentDTO) {
        // 把dto转为do对象
        EquipmentRentDO equipmentRentDO = equipmentRentConvert.dto2do(equipmentDTO);
        int result = equipmentRentMapper.delete(equipmentRentDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("退选成功");
        }else {
            return ResultDTO.buildSuccess("退选失败");
        }
    }
}
